/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.resource.endpoint;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springblade.common.entity.MinioPolicy;
import org.springblade.core.oss.model.OssFile;
import org.springblade.core.tool.api.R;
import org.springblade.resource.core.MinioTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 对象存储端点
 *
 * @author WM
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("oss/endpoint")
@Api(value = "MinIO对象存储", tags = "MinIO对象存储端点")
public class MinIOEndpoint {
	private final MinioTemplate minioTemplate;

	@PostMapping("upload")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "上传图片", notes = "通过文件流上传图片")
	public R<OssFile> upload(MultipartFile file) {
		return R.data(minioTemplate.putFile(file.getOriginalFilename(), file));
	}

	@GetMapping("policy")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取策略", notes = "通过策略直传图片")
	public R<MinioPolicy> getPolicy(@RequestParam String fileName, @RequestParam String contentType) {
		return R.data(minioTemplate.getPolicy(fileName, contentType));
	}


	@DeleteMapping("delete")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "删除图片", notes = "删除图片")
	public R delete(@RequestParam String filePath) {
		minioTemplate.removeFile(filePath);
		return R.success("删除成功");
	}


	@DeleteMapping("deletes")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "删除图片", notes = "删除图片")
	public R deletes(@RequestBody List<String> filePaths) {
		minioTemplate.removeFiles(filePaths);
		return R.success("删除成功");
	}
}
